package com.quanyan.ic.datacenter.http.controller;

import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.common.http.enums.ApiResponseEnum;
import com.quanyan.ic.datacenter.http.req.ReqEventAlarms;
import com.quanyan.ic.datacenter.http.req.ReqHandleAlarm;
import com.quanyan.ic.datacenter.http.resp.RespPlaceLightDetail;
import com.quanyan.ic.datacenter.http.service.AlarmsHandleService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by jian.zhou on 2017/8/24.
 */
@RestController
@RequestMapping("/larkic/icData/handle/")
public class AlarmsHandleController {

    private static final Logger logger = LoggerFactory.getLogger(AlarmsReportController.class);

    @Autowired
    private AlarmsHandleService alarmsHandleService;


    //异常列表
    @RequestMapping("alarmsList")
    public APIResponse queryPlaceAlarmEvent(@Valid @RequestBody ReqEventAlarms reqEventAlarms){
        try {
            return APIResponse.returnSuccess(alarmsHandleService.findEventAlarms(reqEventAlarms));
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }
    }


    //场馆运动类型 提醒 告警
    @RequestMapping("category")
    public APIResponse category(@Valid @RequestBody ReqEventAlarms reqEventAlarms){
        try {
            return APIResponse.returnSuccess(alarmsHandleService.findPlaceCategory(reqEventAlarms));
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }
    }


    //处理异常
    @RequestMapping("alarm")
    public APIResponse alarm(@Valid @RequestBody ReqHandleAlarm reqHandleAlarm){
        try {
            alarmsHandleService.handleAlarm(reqHandleAlarm);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }
    }


    //场馆开灯详情
    @RequestMapping("onLight")
    public APIResponse onLight(@Valid @RequestBody ReqEventAlarms reqEventAlarms){
        try {
            return APIResponse.returnSuccess(new RespPlaceLightDetail());
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }
    }


    //场馆是否有未处理的异常
    @RequestMapping("isHaveAlarm")
    public APIResponse isHaveAlarm(@Valid @RequestBody ReqEventAlarms reqEventAlarms){
        Integer placeId = reqEventAlarms.getPlaceId();
        try {
            return APIResponse.returnSuccess(alarmsHandleService.isHaveAlarm(placeId));
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail(ApiResponseEnum.INTERNAL_ERROR);
        }
    }

    @RequestMapping("types")
    public APIResponse handleTypes(@RequestBody Map<String, Object> map){
        Byte category = MapUtils.getByte(map, "category");
        if (category == null){
            category = 0; //全部
        }
        return APIResponse.returnSuccess(
                alarmsHandleService.getHandleTypes(category));
    }
}
